package com.esun.server.exceptionHandler;

import com.esun.common.exception.AsyncException;
import com.esun.common.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import java.lang.reflect.Method;

/**
 * 异步异常处理器
 *
 * @author John.xiao
 * @date 2021/3/8 11：17
 */
//@RestControllerAdvice
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
	private final static Logger logger = LoggerFactory.getLogger(AsyncExceptionHandler.class);

	@Override
	public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
		logger.info("Async method has uncaught exception, params: " + objects);
		logger.error("Exception :", throwable);
		if (throwable instanceof AsyncException) {
			AsyncException asyncException = (AsyncException) throwable;
			logger.info("asyncException:" + asyncException.getMsg());
		}
		throw new CustomException(throwable.getMessage());
	}


}
